<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>解构赋值的作用</title>
  </head>
  <body>
    <script>
      /**
       * swap操作
       */

      let x = 10;
      let y = 20;
      [x, y] = [y, x];
      console.log(x, y);

      /**
       * 函数返回如果是对象或者数组类型特别好用
       */

      function getObj() {
        return {
          name: "Alice",
          age: 20,
        };
      }

      function getArray() {
        return [1, 2, 3, 4, 5, 6, 7, 8, 9];
      }

      let { name, age } = getObj();
      console.log(name, age);

      let [a, b, c, d, ...arr] = getArray();
      console.log(a, b, c, d, arr);

      /**
       * 方便提取JSON中的值
       */

      console.log("----------------------------");

      {
        let jsonData = {
          name: "Tom",
          age: 12,
          nums: [1, 2, 3, 4, 5, 6, 7],
          items: [
            { id: 1, name: "a" },
            { id: 2, name: "b" },
            { id: 3, name: "c" },
          ],
        };

        let { name, age, nums, items } = jsonData;
        console.log(name);
        console.log(age);
        console.log(nums);
        console.log(items);
      }

      /**
       * 遍历 Map 结构
      */
      let map = new Map();
      map.set(1,'a');
      map.set(2,'b');
      map.set(3,'c');

      for(let [key,value] of map){
          console.log(key,value);
      }

    </script>
  </body>
</html>
